package org.turbobytes.unikey.util.db.metadata;

import org.turbobytes.unikey.util.StringUtil;
import org.turbobytes.unikey.util.db.DbException;

import java.util.List;
import java.util.ArrayList;
import java.sql.*;

/**
 * Db metadata operations specific to Oracle
 *
 * @author Ganeshji Marwaha
 * @since Aug 7, 2010
 */
public class OracleMetadata extends GenericMetadata {

  private static final String ORACLE_TABLES = "select object_name from user_objects where object_type = 'TABLE'";

  /**
   * DatabaseMetaData.getTables() doesn't work in case of oracle. So, using oracle specific way to retrieve
   * the list of table names here.
   * @param connection
   * @return
   * @throws Exception
   */
  public List<String> getTableNames(Connection connection) {
    List<String> tables = new ArrayList<String>();

    try {
      Statement stmt = connection.createStatement();
      ResultSet rs = stmt.executeQuery(ORACLE_TABLES);
      while (rs.next()) {
        String tableName = StringUtil.trim(rs.getString(1));
        if(StringUtil.isNotBlank(tableName)) {
          tables.add(tableName);
        }
      }
      return tables;
    } catch (SQLException e) {
      throw new DbException(e);
    }
  }  
}
